function [E,ET,TE] = getmesh(V,T)
% function [E,ET,TE] = getmesh(V,T)
% find the neighbouring informations for simple mesh composed of V and T

% the areas of all triangles.
x21 = V(T(:,2),1) - V(T(:,1),1);
x31 = V(T(:,3),1) - V(T(:,1),1);
y21 = V(T(:,2),2) - V(T(:,1),2);
y31 = V(T(:,3),2) - V(T(:,1),2);
areas = 0.5*(x21.*y31 - x31.*y21);
% promising the index of vertices is anti-clock
pos = find(areas<0); % find the anti-direction triangles
tmp = T(pos,2); % and change their position;
T(pos,2) = T(pos,3);
T(pos,3) = tmp;
%areas(pos) = -1*areas(pos);

% first, reserve spaces for E ET TE .
nt = size(T,1);
E = zeros(nt*3,2);  posE = 0;
ET = zeros(nt*3,2); 
TE = zeros(nt,3);
%  tic;
for i = 1:nt
    for j = 1:3  % check over it's three edges
        v1 =  T(i,mod(j,3)+1);
        v2 =  T(i,mod(mod(j,3)+1,3)+1);
        edge = [min(v1,v2) , max(v1,v2)];
        edgenum = [];
        if posE > 0 % have to search the array E to check if it has existed
            edgenum = find((edge(1) == E(:,1))&(edge(2) == E(:,2)));
        end
        if isempty(edgenum) 
            posE = posE + 1; 
            E(posE,:) = edge;
            edgenum = posE;
            ET(edgenum,1) = i; % the eg is new, I'm his father.
        else  % the eg has exist, then I'm the mother of this edge;
            ET(edgenum,2) = i; 
        end
        TE(i,j) = edgenum;
    end
end
E = E(1:posE,:); ET = ET(1:posE,:);
% bdr = find(ET(1:posE,2)==0);